package study.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/testRequest")
public class RequestTestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("RequestTestServlet======>>>");

        req.setAttribute("myrequest","hello request");
        //请求转发 forword()  控制权交转发的对象

        //req.getRequestDispatcher("/getRequest").forward(req,resp);

        //请求转发include()   控制权暂时交给你，你处理完毕后，请转交会给我，我一起响应回去
       // req.getRequestDispatcher("/getRequest").include(req,resp);

        //resp.getWriter().print("RequestTestServlet===>>");

        //使用重定向
        //原理： 两个步骤  1 设置状态码为  302
        //               2 设置响应头（Location:重新定位的url）
        // resp.sendRedirect("getRequest");

        HttpSession session = req.getSession();

        String string1 = (String) session.getAttribute("mysession");

        resp.getWriter().print("RequestTestServlet===>>session===="+string1);

    }
}
